package com.example.texted.optional;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Optional;

public class optionalCar {

    public class Person {
        private Optional<Car> car;

        public Optional<Car> getCar() {
            return car;
        }

        public void setCar(Car car) {
            this.car = Optional.of(car);
        }
    }

    public class Car {
        private Optional<Insurance> insurance;

        public Optional<Insurance> getInsurance() {
            return insurance;
        }

        public void setInsurance(Insurance insurance) {
            this.insurance = Optional.of(insurance);
        }
    }

    public class Insurance {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public String getCarInsuranceName(Optional<Person> person) {
        return person.flatMap(Person::getCar)
                .flatMap(Car::getInsurance)
                .map(Insurance::getName)
                .orElse("Unknown")
                ;
    }

    @Test
    public void Do() {
        Optional<Person> person = Optional.empty();
//        Car car = new Car();
//        Insurance insurance = new Insurance();
//        insurance.setName("666");
//        car.setInsurance(insurance);
//        Person person1 = person.orElse(new Person());
//        person1.setCar(car);
//        Optional<Person> person11 = Optional.of(person1);
//        System.out.println(getCarInsuranceName(person11));

//        Optional<String> name =
//                person.map(Person::getCar)
//                        .map(Car::getInsurance)
//                        .map(Insurance::getName)
//                ;
    }
}
